package com.xinxin.o2o.controller.productcontroller;

import com.xinxin.o2o.dto.ProductCategoryExecution;
import com.xinxin.o2o.enums.GlobalCode;
import com.xinxin.o2o.enums.ProductCategoryStateEnum;
import com.xinxin.o2o.model.ProductCategory;
import com.xinxin.o2o.response.Response;
import com.xinxin.o2o.service.ProductCategoryService;
import com.xinxin.o2o.util.ResponseUtil;
import com.xinxin.o2o.vo.productcategory.ProductCategoryListParam;
import com.xinxin.o2o.vo.productcategory.ProductCategoryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 史鑫鑫
 * @date 2019-08-19 17:41
 */
@Api(tags = "商品类别管理模块")
@RestController
@RequestMapping("/api/productcategory")
public class ProductCategoryManagementController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @ApiOperation(value = "获取商品类别列表")
    @GetMapping("/queryCategory")
    public Response queryCategory(@RequestParam("shopId") Long shopId) {
        return ResponseUtil.success(productCategoryService.queryProductCategoryList(shopId));
    }

    @ApiOperation(value = "添加商品类别")
    @PostMapping("/addProductCategories")
    public Response addProductCategories(@RequestBody ProductCategoryListParam productCategoryListParam) {
        List<ProductCategory> productCategoryList = productCategoryListParam.getProductCategoryList();
        if (productCategoryList != null && productCategoryList.size() > 0) {
            try {
                ProductCategoryExecution productCategoryExecution = productCategoryService.batchAddProductCategory(productCategoryList);
                if (productCategoryExecution.getState() == ProductCategoryStateEnum.SUCCESS.getState()) {
                    return ResponseUtil.success();
                } else {
                    return ResponseUtil.error(GlobalCode.INNER_ERROR, productCategoryExecution.getStateInfo());
                }
            } catch (RuntimeException e) {
                return ResponseUtil.error(GlobalCode.RUN_TIME_EXCEPTION, e.getMessage());
            }
        } else {
            return ResponseUtil.error(GlobalCode.PARAM_IS_EMPTY, "请至少输入一个商品类别");
        }
    }

    @ApiOperation(value = "删除商品类别")
    @DeleteMapping("/deleteProductCategory")
    public Response deleteProductCategory(@RequestBody ProductCategoryParam productCategoryParam) {
        ProductCategoryExecution productCategoryExecution = productCategoryService.deleteProductCategory(productCategoryParam.getProductCategoryId(), productCategoryParam.getShopId());
        if (productCategoryExecution.getState() == ProductCategoryStateEnum.SUCCESS.getState()) {
            return ResponseUtil.success();
        } else {
            return ResponseUtil.error(GlobalCode.DELETE_PRODUCT_CATEGORY_ERROR, productCategoryExecution.getStateInfo());
        }
    }
}
